go to previous page   go to home page   go to next page

Answer:

Yes.


Using a Super Class's Constructor

// constructor
public Movie( String ttl, int lngth, String dir, String rtng )
{
  super(ttl, lngth);         // use the base class's constructor to initialize members inherited from it
  director = dir;            // initialize the members new to Movie
  rating = rtng;  
}

Look at he constructor for class Movie. The class definition for Video has a constructor that initializes the member data of Video objects. The class Movie has a constructor that initializes the data of Movie objects.

The statement super(ttl, lngth) invokes a constructor of the parent to initialize some of the data. There are two constructors in the parent. The one that is invoked is the one that matches the argument list in super(ttl, lngth). Then the next two statements initialize the members that only Movie has.

Important Note: super() must be the first statement in the subclass's constructor.

This fact is often overlooked and may cause mysterious compiler error messages.


QUESTION 11:

Why is the statement that invokes the parent's constructor called super()?